package com.greedy;

import java.util.Arrays;

public class Solution11 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int cur = 0;
        int total = 0;
        int start = 0;
        for (int i = 0; i < cost.length; i++) {
            cur += gas[i]-cost[i];
            total += gas[i]-cost[i];
            if (cur<0) {
                start = i+1;
                cur = 0;
            }
        }
        if (total <0) return -1;
        return start;
    }
}
